#include "CMain.h"
#include <iostream>
#include "test_task.h"
#include "CListenerCP.h"
#include "EasyPacketQueueMgr.h"
#include "EasyLog.h"

#ifdef WIN32
#include <Windows.h>
#else
#include <syswait.h>
#endif

using namespace std;


#define  CHECK_AND_INIT(p)\
	if (p==NULL)\
	{\
		return 1;\
	}\
	if (!p->init())\
	{\
		return 1;\
	}

#define  UNINIT_AND_DELETE(p)\
	if (p!=NULL)\
	{\
		p->uninit();\
		delete p;\
		p = NULL;\
	}


int main(int argc, char** argv)
{
	CTestTask * pTestTask = new CTestTask(1);
	CHECK_AND_INIT(pTestTask);
	CEasyPacketMgr* pEasyPacketMgr = new CEasyPacketMgr(4);
	CHECK_AND_INIT(pEasyPacketMgr);
	CListenerCP* pListenerCP = new CListenerCP(3, "127.0.0.1", 9090);
	pListenerCP->setPacketMgr(pEasyPacketMgr);
	CHECK_AND_INIT(pListenerCP);
	pEasyPacketMgr->setListener(pListenerCP);
	CLog2Sqlite* pLog = new CLog2Sqlite(5);
	CHECK_AND_INIT(pLog);
	CMain* pMain = new CMain(2);
	CHECK_AND_INIT(pMain);
	pMain->addTask(pListenerCP);
	pMain->addTask(pTestTask);
	pMain->addTask(pEasyPacketMgr);
	pMain->addTask(pLog);

	while(1)
	{
#ifdef WIN32
		Sleep(1000);
#else
		sleep(1);
#endif
		pMain->run();
	}
	UNINIT_AND_DELETE(pMain);
	UNINIT_AND_DELETE(pLog);
	UNINIT_AND_DELETE(pListenerCP);
	UNINIT_AND_DELETE(pEasyPacketMgr);
	UNINIT_AND_DELETE(pTestTask);
	return 0;
}